package com.zrrd.yunchmall.product.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.pojo.ProductTmp;
import com.zrrd.yunchmall.product.entity.Product;
import com.zrrd.yunchmall.product.service.IProductService;
import com.zrrd.yunchmall.product.service.impl.ProductServiceImpl;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.CredentialNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * @author yekong
 */
//http://id:port/product/{pid} => Restful
@RestController
@RequestMapping("/product")
@SuppressWarnings("all")
@Api(tags = "商品服务-商品管理模块")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     *
     * @return
     */
    @ApiOperation("查询商品列表")
    @GetMapping("/list")
    public ResponseResult list(@ApiParam("商品名称关键字") String keyword,
                               @RequestParam int pageNum,
                               @RequestParam int pageSize,
                               @ApiParam("上架状态") Integer publishStatus,
                               @ApiParam("审核状态") Integer verifyStatus,
                               @ApiParam("商品编码") String productSn,
                               @ApiParam("商品类别ID") Integer productCategoryId,
                               @ApiParam("品牌ID") Integer brandId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (!StringUtil.isEmpty(keyword)) {
            //模糊查询
            queryWrapper.like("name", keyword);
        }
        if (publishStatus != null) queryWrapper.eq("publish_status", publishStatus);
        if (verifyStatus != null) queryWrapper.eq("verify_status", verifyStatus);
        if (!StringUtil.isEmpty(productSn)) queryWrapper.eq("product_sn", productSn);
        if (productCategoryId != null) queryWrapper.eq("product_category_id", productCategoryId);
        if (brandId != null) queryWrapper.eq("brand_id", brandId);

        return new ResponseResult(200, "查询成功",
                productService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }

    @ApiOperation("删除商品")
    @PostMapping("/update/deleteStatus")
    public ResponseResult updateDeleteStatus(@ApiParam("删除状态") @RequestParam int deleteStatus,
                                             @ApiParam("删除商品的id") @RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("delete_status", deleteStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200, "删除成功");
    }

    @ApiOperation("商品是否为新品")
    @PostMapping("/update/newStatus")
    public ResponseResult updateNewStatus(@ApiParam("新品状态") @RequestParam int newStatus,
                                             @ApiParam("商品的id") @RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("new_status", newStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200, "修改新品状态成功");
    }

    @ApiOperation("商品是否推荐")
    @PostMapping("/update/recommendStatus")
    public ResponseResult updateRecommendStatus(@ApiParam("推荐状态") @RequestParam int recommendStatus,
                                          @ApiParam("商品的id") @RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("recommand_status", recommendStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200, "修改推荐状态成功");
    }

    @ApiOperation("商品是否上架")
    @PostMapping("/update/publishStatus")
    public ResponseResult updatePublishStatus(@ApiParam("上架状态") @RequestParam int publishStatus,
                                                @ApiParam("商品的id") @RequestParam String ids) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.set("publish_status", publishStatus);
        updateWrapper.in("id", ids.split(","));
        productService.update(updateWrapper);
        return new ResponseResult<>(200, "修改上架状态成功");
    }

    @ApiOperation("新增商品信息")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Product product) {
        productService.save(product);
        return new ResponseResult(200, "添加成功");
    }

    @ApiOperation("查询商品详情")
    @GetMapping("/updateInfo/{id}")
    public ResponseResult updateProduct(@PathVariable("id") int id) {
        return new ResponseResult(200,"查询成功", productService.getById(id));
    }

    @ApiOperation("修改商品")
    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") long id, @RequestBody Product product) {
        productService.updateProduct(id, product);
        return new ResponseResult(200, "修改成功");
    }

    @ApiOperation("退货关闭订单释放内存")
    @PostMapping("/stock/free")
    public ResponseResult freeStock(@RequestBody List<Map<String, Long>> params) {
        productService.freeStock(params);
        return new ResponseResult(200, "执行成功");
    }



    @GetMapping("/testSleuth")
    public String testSleuth() {
        return "商品：测试链路追踪";
    }
}
